#include "main.h"

int main(void)
{
	int inputcode,ret;
	int fd;
	MSG msg;

	/*与服务器建立连接*/
	struct sockaddr_in server;
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = inet_addr("192.168.104.99");
	server.sin_port = htons(12001);	

	/*用户界面*/
	START:
	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd == -1) 
	{
		perror("socket");
		exit(1);
	}

	ret = connect(fd, (struct sockaddr*)&server, sizeof(server));	
	if (ret == -1)
	{
		perror("connect");
		exit(1);
	}
	
	printf_user();
	printf(">>输入命令数字：");
	scanf("%d",&inputcode);
	fflush(stdin);

	if(inputcode == 0)
	{
		query_exit(fd,&msg);
		return 0;
	}
	else if(inputcode == 1)
	{
		ret = login(fd,&msg);
		if(ret == 0)
			goto QUERY;//登录成功则进入查询界面
		else
			goto START;
	}
	else if(inputcode == 2)
	{
		regis(fd,&msg);
		goto START;//无论注册成功与否都回到用户界面
	}
	else
	{
		printf("输入的命令无法识别，请重新输入。\n");
		goto START;
	}

	/*查询界面*/
	QUERY:
	fd = socket(AF_INET, SOCK_STREAM, 0);
	if (fd == -1) 
	{
		perror("socket");
		exit(1);
	}

	ret = connect(fd, (struct sockaddr*)&server, sizeof(server));	
	if (ret == -1)
	{
		perror("connect");
		exit(1);
	}
	
	printf_query();
	printf(">>输入命令数字：");
	scanf("%d",&inputcode);
	fflush(stdin);

	if(inputcode == 0)
	{
		query_exit(fd,&msg);
		return 0;
	}
	else if(inputcode == 1)
		query_word(fd,&msg);
	else if(inputcode == 2)
		query_history(fd,&msg);
	else
		printf("输入的命令无法识别，请重新输入。\n");
	
	goto QUERY;
	
	return 0;
}
